package com.bhs.sansonglogistics.ui.mine;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.Department;
import com.bhs.sansonglogistics.bean.DepartmentData;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 员工管理
 */
public class StaffManagementActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private int page=1;
    private BaseQuickAdapter<Department, BaseViewHolder> mAdapter;

    @Override
    public int initLayout() {
        return R.layout.activity_staff_management;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("员工管理");
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
        findViewById(R.id.btn_add).setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAdapter=new BaseQuickAdapter<Department, BaseViewHolder>(R.layout.item_department) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, Department item) {
                helper.setText(R.id.tv_department,item.getDepartment_name());
                helper.setText(R.id.tv_manager,item.getDepartment_contact()+" "+item.getDepartment_mobile());
                helper.addOnClickListener(R.id.tv_staff_management,R.id.tv_edit);
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(mActivity,AddDepartmentActivity.class)
                        .putExtra("department",mAdapter.getData().get(position)));
            }
        });

        //监听 刷新是回调
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
    }

    @Override
    protected void onResume() {
        super.onResume();
        page=1;
        loadData();
    }

    private void loadData() {
        networkRequest(netApi.get_department_list(page,10),this,888);
    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(mActivity,AddDepartmentActivity.class));
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        DepartmentData data=new Gson().fromJson(json,DepartmentData.class);
        mRefreshLayout.setRefreshing(false);
        if(data.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(data.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(data.getData().getList());
            }
            if (mAdapter.getData().size() >= data.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(data.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}